package com.xiaotu.utils;

import java.io.UnsupportedEncodingException;

/**
 * @author zhangxiao
 * @create 2021-02-19 15:04
 */
public class FullCharConverter {

    // 全角转半角的 转换函数
    public static String halfChange(String QJstr)
            throws UnsupportedEncodingException {

        StringBuffer outStrBuf = new StringBuffer("");

        String Tstr = "";

        byte[] b = null;

        for (int i = 0; i < QJstr.length(); i++) {

            Tstr = QJstr.substring(i, i + 1);

            // 全角空格转换成半角空格

            if (Tstr.equals("　")) {

                outStrBuf.append(" ");

                continue;

            }

            b = Tstr.getBytes("unicode");

            // 得到 unicode 字节数据

            if (b[2] == -1) {

                // 表示全角？

                b[3] = (byte) (b[3] + 32);

                b[2] = 0;

                outStrBuf.append(new String(b, "unicode"));

            } else {

                outStrBuf.append(Tstr);

            }

        } // end for.

        return outStrBuf.toString();

    }

    // 半角转全角
    public static String fullchange(String QJstr)
            throws UnsupportedEncodingException {

        StringBuffer outStrBuf = new StringBuffer("");

        String Tstr = "";

        byte[] b = null;

        for (int i = 0; i < QJstr.length(); i++) {

            Tstr = QJstr.substring(i, i + 1);

            if (Tstr.equals(" ")) {

                // 半角空格

                outStrBuf.append("　");

                continue;

            }

            b = Tstr.getBytes("unicode");

            if (b[2] == 0) {

                // 半角?

                b[3] = (byte) (b[3] - 32);

                b[2] = -1;

                outStrBuf.append(new String(b, "unicode"));

            } else {

                outStrBuf.append(Tstr);

            }

        }

        return outStrBuf.toString();

    }


    public static void main(String[] args) throws UnsupportedEncodingException {
        String str1 = "我是123#";
        String str2 = "我是１２３＃";
        String halfChange1 = halfChange(str1);
        String halfChange2 = halfChange(str2);
        System.out.println(halfChange1.equals(halfChange2));
//        String toSBCS = half2Fullchange(str1);
//        System.out.println(toSBCS);
//        System.out.println(toSBCS.equals(str2));
    }

}
